package com.yaorange.module.system.controller;

import com.aliyuncs.exceptions.ClientException;
import com.yaorange.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/upload")
@Slf4j
public class UploadController {
    @Autowired
    Uploads uploads;
    @PostMapping
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException, ClientException {
        log.info("上传文件:{}",file.getOriginalFilename());
        String originalFilename = file.getOriginalFilename();
        String rename = uploads.rename(originalFilename);
        uploads.upload(rename, file.getBytes());
        return Result.success("https://teachingsys.oss-cn-chengdu.aliyuncs.com/"+rename);
    }

}
